package service

import (
	"core/db"
	"core/entity"
	"core/helper"
)

func ListProjectGroups() (projectGroups []entity.ProjectGroup, err error) {
	err = db.DB.Find(&projectGroups).Error
	if err != nil {
		return nil, err
	}
	return projectGroups, nil
}

func FindProjectPage(pageNum int, pageSize int, projectGroupId string) (page *helper.Page, err error) {

	return nil, nil
}

func CreateProjectGroup(projectGroup *entity.ProjectGroup) (err error) {
	err = db.DB.Create(projectGroup).Error
	if err != nil {
		return err
	}
	return nil
}

func DeleteProjectGroup(id string) (err error) {
	err = db.DB.Delete(&entity.ProjectGroup{ID: id}).Error
	if err != nil {
		return err
	}
	return nil
}

func UpdateProjectGroup(id string, pprojectGroup *entity.ProjectGroup) (err error) {
	var projectGroup entity.ProjectGroup
	err = db.DB.Where("id = ?", id).First(&projectGroup).Error
	if err != nil {
		return err
	}

	projectGroup.IsShow = pprojectGroup.IsShow
	projectGroup.Description = pprojectGroup.Description
	projectGroup.Name = pprojectGroup.Name

	err = db.DB.Save(projectGroup).Error
	if err != nil {
		return err
	}

	return nil
}

func UpdateShowProjectGroup(id string) (err error) {
	var projectGroup entity.ProjectGroup
	err = db.DB.Where("id = ?", id).First(&projectGroup).Error
	if err != nil {
		return err
	}

	projectGroup.IsShow = !projectGroup.IsShow

	err = db.DB.Save(projectGroup).Error
	if err != nil {
		return err
	}

	return nil
}
